﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TCKQKL.Service.LookupClient;
using TCKQKL.FrontEnd.DataModel;
using System.Collections.Generic;

namespace TCKQKL.FrontEnd.Web.Controls
{
    public partial class AccountInfoControl : System.Web.UI.UserControl
    {
        public List<StockHoldingUI> holding = new List<StockHoldingUI>();
        public decimal Preloan = 0;
        public List<BankDataUI> cash = new List<BankDataUI>();
        public List<DateTime> backDate = new List<DateTime>();
        public InvestorDataProfileUI investor { set; get; }

        public decimal Trading = 0;
        public decimal Netting = 0;
        public decimal Blocking = 0;
        public decimal Total = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            LSClient client = new LSClient();

            backDate = client.getBackDate();
            decimal investorID = client.getAccountData(HttpContext.Current.User.Identity.Name).investorId;

            List<AccountDataUI> T0 = client.getAccountByInvestorId(investorID);

            List<AccountDataUI> T1 = client.getStockNettingInvestorId(investorID, backDate[3]);
            List<AccountDataUI> T2 = client.getStockNettingInvestorId(investorID, backDate[2]);
            List<AccountDataUI> T3 = client.getStockNettingInvestorId(investorID, backDate[1]);

            cash = client.getBankDataByInvestorId(investorID);
            Preloan = client.getPreLoanByInvestor(investorID);
            client.Close();
            holding = MergeHolding(T0, T1, T2, T3);

            foreach (StockHoldingUI item in holding)
            {
                Trading += item.TradingValue;
                Netting += item.NettingValue;
                Blocking += item.BlockingValue;
                Total += item.Value;
            }

        }

        private List<StockHoldingUI> MergeHolding(List<AccountDataUI> T0, List<AccountDataUI> T1, List<AccountDataUI> T2, List<AccountDataUI> T3)
        {
            List<StockHoldingUI> list = new List<StockHoldingUI>();

            foreach (AccountDataUI stock in T0)
            {
                StockHoldingUI item = new StockHoldingUI();
                LSClient client = new LSClient();

                item.Symbol = stock.stockId;
                item.Name = stock.stockName;
                item.TradingQtty = stock.tradingAmount;
                item.BlockingQtty = stock.lockedAmount;

                QuoteDataUI quote = client.getQuote(stock.stockId);
                item.RefPrice = quote.Closed;
                client.Close();
                foreach (AccountDataUI stockt1 in T1)
                {
                    if (stock.stockId.Equals(stockt1.stockId))
                        item.NettingT1 = stockt1.tradingAmount;
                }
                foreach (AccountDataUI stockt2 in T2)
                {
                    if (stock.stockId.Equals(stockt2.stockId))
                        item.NettingT2 = stockt2.tradingAmount;
                }
                foreach (AccountDataUI stockt3 in T3)
                {
                    if (stock.stockId.Equals(stockt3.stockId))
                        item.NettingT3 = stockt3.tradingAmount;
                }
                list.Add(item);
            }
            return list;
        }
    }
}